package com.granada.behaviour.base;

import com.granada.entity.SysUserRole;
import com.granada.utils.MapperBeanUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public abstract class UserRoleBehavior implements com.granada.behaviour.interfaces.UserRoleBehavior {

    public void removeByRoleId(Long roleId) {
        MapperBeanUtils.getSysUserRoleMapper().removeByRoleId(roleId);
    }

    @Override
    public List<Long> getRoleIdsByUserId(Long userId) {
        return MapperBeanUtils.getSysUserRoleMapper().getRoleIdsByUserId(userId);
    }

    @Transactional(rollbackFor = Exception.class)
    public void addBatch(List<Long> roleIds, Long userId) {
            Date createTime=new Date();
            List<SysUserRole> list=new ArrayList<>();
            for (Long roleId: roleIds){
                SysUserRole sysUserRole = SysUserRole.builder()
                    .id(null)
                    .createTime(new Date())
                    .userId(userId)
                    .roleId(roleId)
                    .build();
                list.add(sysUserRole);
            }
            MapperBeanUtils.getSysUserRoleMapper().removeByUserId(userId);
            MapperBeanUtils.getSysUserRoleMapper().batchUserRole(list);
    }

    @Override
    public void removeByUserId(Long userId) {
        MapperBeanUtils.getSysUserRoleMapper().removeByUserId(userId);
    }

    @Override
    public List<Long> getUserIdsByRoleIds(List<Long> roleIds) {
        return MapperBeanUtils.getSysUserRoleMapper().getUserIdsByRoleIds(roleIds);
    }
}
